package com.xijia.wuyouwallet.activity;

import android.content.Intent;
import android.os.Bundle;
import android.support.annotation.Nullable;
import android.view.View;
import android.widget.AdapterView;
import android.widget.ImageView;
import android.widget.ListView;
import android.widget.TextView;

import com.alibaba.fastjson.JSONObject;
import com.xijia.wuyouwallet.MyApp;
import com.xijia.wuyouwallet.R;
import com.xijia.wuyouwallet.adapter.BankListAdapter;
import com.xijia.wuyouwallet.api.MyCallBack;
import com.xijia.wuyouwallet.base.BaseActivity;
import com.xijia.wuyouwallet.bean.BankListData;
import com.xijia.wuyouwallet.utils.Constants;
import com.xijia.wuyouwallet.utils.MyLogger;
import com.xijia.wuyouwallet.utils.UrlConfig;
import com.xijia.wuyouwallet.utils.XUtil;
import com.xijia.wuyouwallet.widget.ErrorHintView;

import java.util.ArrayList;
import java.util.HashMap;
import java.util.List;
import java.util.Map;

/**
 * Created by Administrator on 2017/9/13 0013.
 */

public class ChoiceBankActivity extends BaseActivity implements View.OnClickListener{

    private TextView choice_bank_title;
    private ImageView choice_bank_back,choice_bank_add;
    private ListView choice_bank_lv;
    private ErrorHintView mErrorHintView;

    private List<BankListData.DataBean.ListBean> list;
    private BankListAdapter adapter;

    public static final int VIEW_CONTENT = 1;//view显示
    public static final int VIEW_WIFIFAILUER = 2;//显示断网
    public static final int VIEW_LOADFAILURE = 3;//加载失败
    public static final int VIEW_LOADING = 4;//加载
    public static final int VIEW_NODATA = 5;//无数据

    private String cardType;

    private int postation;

    @Override
    protected void onCreate(@Nullable Bundle savedInstanceState) {
        super.onCreate(savedInstanceState);
        setContentView(R.layout.activity_choice_bank);
        initView();
    }

    private void initView(){
        choice_bank_title = (TextView) findViewById(R.id.choice_bank_title);
        choice_bank_back = (ImageView) findViewById(R.id.choice_bank_back);
        choice_bank_back.setOnClickListener(this);
        choice_bank_add = (ImageView) findViewById(R.id.choice_bank_add);
        choice_bank_add.setOnClickListener(this);
        choice_bank_lv = (ListView) findViewById(R.id.choice_bank_lv);
        mErrorHintView = (ErrorHintView) findViewById(R.id.mErrorHintView);

        cardType = getIntent().getStringExtra("cardType");

        list = new ArrayList<>();
        adapter = new BankListAdapter(list,this);
        choice_bank_lv.setAdapter(adapter);
        choice_bank_lv.setOnItemClickListener(new AdapterView.OnItemClickListener() {
            @Override
            public void onItemClick(AdapterView<?> adapterView, View view, int i, long l) {
                if (MyApp.isRefund){
                    postation = i;
                    if (list.get(i).getCard_cvn2().equals("0")){
                        Bundle bundle = new Bundle();
                        bundle.putString("card_id",list.get(i).getCard_id());
                        bundle.putString("card_merch_mobile",list.get(i).getCard_merch_mobile());
                        jumpToPage(BankCvnActivity.class,bundle,true,Constants.REQUEST_DATA,false);
                    }else {
                        if (list.get(i).getCard_merch_mobile().equals("")){
                            requestMerchant();
                        }else {
                            MyApp.isRefund = false;
                            Intent intent = new Intent();
                            intent.putExtra("img",list.get(postation).getBank_logo());
                            intent.putExtra("name",list.get(postation).getCard_bank());
                            intent.putExtra("num",list.get(postation).getCard_number());
                            intent.putExtra("cardId",list.get(postation).getCard_id());
                            intent.putExtra("cardType",list.get(postation).getCard_type());
                            ChoiceBankActivity.this.setResult(Constants.RESULT_DATA,intent);
                            ChoiceBankActivity.this.finish();
                        }
                    }
                }else {
                    Intent intent = new Intent();
                    intent.putExtra("img",list.get(i).getBank_logo());
                    intent.putExtra("name",list.get(i).getCard_bank());
                    intent.putExtra("num",list.get(i).getCard_number());
                    intent.putExtra("cardId",list.get(i).getCard_id());
                    intent.putExtra("cardType",list.get(i).getCard_type());
                    ChoiceBankActivity.this.setResult(Constants.RESULT_DATA,intent);
                    ChoiceBankActivity.this.finish();
                }
            }
        });
        initData();
        showLoading(VIEW_LOADING);
        requestData(true);

    }
    private void requestMerchant(){
        Map<String,Object> map = new HashMap<>();
        map.put("user", MyApp.userData.getData().getUser_id());
        map.put("card_id",list.get(postation).getCard_id());
        map.put("token",MyApp.userData.getData().getToken());
        showProgressDialog(true);
        XUtil.Post(UrlConfig.CREATE_MERCHANT_URL,map,new MyCallBack<String>(){

            @Override
            public void onSuccess(String result) {
                super.onSuccess(result);
                dismissProgressDialog();
                MyLogger.wLog().e("createMerchant ="+result);
                JSONObject jsonObject = JSONObject.parseObject(result);
                if (jsonObject.getInteger("code")==200){
                    MyApp.isRefund = false;
                    Intent intent = new Intent();
                    intent.putExtra("img",list.get(postation).getBank_logo());
                    intent.putExtra("name",list.get(postation).getCard_bank());
                    intent.putExtra("num",list.get(postation).getCard_number());
                    intent.putExtra("cardId",list.get(postation).getCard_id());
                    intent.putExtra("cardType",list.get(postation).getCard_type());
                    ChoiceBankActivity.this.setResult(Constants.RESULT_DATA,intent);
                    ChoiceBankActivity.this.finish();
                }else {
                    showToast(jsonObject.getString("msg"));
                    finish();
                }
            }

            @Override
            public void onError(Throwable ex, boolean isOnCallback) {
                super.onError(ex, isOnCallback);
                dismissProgressDialog();
                showToast("服务器出错啦");
                MyLogger.wLog().e("createMerchant onError="+ex.toString());
            }
        });
    }
    private void initData(){
        if (cardType.equals("0")){
            choice_bank_title.setText("选择信用卡");
        }else if (cardType.equals("1")){
            choice_bank_title.setText("选择银行卡");
        }
    }

    @Override
    protected void onActivityResult(int requestCode, int resultCode, Intent data) {
        super.onActivityResult(requestCode, resultCode, data);
        if (requestCode==Constants.REQUEST_DATA&&resultCode==Constants.RESULT_DATA){
            MyApp.isRefund = false;
            Intent intent = new Intent();
            intent.putExtra("img",list.get(postation).getBank_logo());
            intent.putExtra("name",list.get(postation).getCard_bank());
            intent.putExtra("num",list.get(postation).getCard_number());
            intent.putExtra("cardId",list.get(postation).getCard_id());
            intent.putExtra("cardType",list.get(postation).getCard_type());
            ChoiceBankActivity.this.setResult(Constants.RESULT_DATA,intent);
            ChoiceBankActivity.this.finish();
        }
    }

    @Override
    protected void onResume() {
        super.onResume();
        if (MyApp.isChoiseCard){
            MyApp.isCard = false;
            MyApp.isChoiseCard = false;
            showLoading(VIEW_LOADING);
            requestData(true);
        }
    }

    private void requestData(final boolean isFirst){
        Map<String,Object> map = new HashMap<>();
        map.put("token", MyApp.userData.getData().getToken());
        map.put("user",MyApp.userData.getData().getUser_id());
        map.put("type",cardType);
        XUtil.Post(UrlConfig.GET_BANKCARD_LIST,map,new MyCallBack<String>(){

            @Override
            public void onSuccess(String result) {
                super.onSuccess(result);
                MyLogger.wLog().e("获取银行卡列表result="+result);
                BankListData data = JSONObject.parseObject(result,BankListData.class);
                if (data.getCode()==200){
                    if (isFirst){
                        if (!list.isEmpty()){
                            list.clear();
                        }
                        showLoading(VIEW_CONTENT);
                    }
                    for (BankListData.DataBean.ListBean listBean : data.getData().getList()){
                        list.add(listBean);
                    }
                    adapter.notifyDataSetChanged();
                }else {
                    if (isFirst){
                        showLoading(VIEW_NODATA);
                    }
                }
            }

            @Override
            public void onError(Throwable ex, boolean isOnCallback) {
                super.onError(ex, isOnCallback);
                //MyLogger.wLog().e("获取银行卡列表onError="+ex.toString());
                if (isFirst){
                    showLoading(VIEW_LOADFAILURE);
                }
            }
        });
    }

    //显示对话框
    private void showLoading(int i) {
        choice_bank_lv.setVisibility(View.GONE);
        mErrorHintView.setVisibility(View.GONE);
        switch (i) {
            case VIEW_CONTENT:
                mErrorHintView.hideLoading();
                choice_bank_lv.setVisibility(View.VISIBLE);
                break;
            case VIEW_WIFIFAILUER:
                mErrorHintView.hideLoading();
                mErrorHintView.netError(new ErrorHintView.OperateListener() {
                    @Override
                    public void operate() {
                        showLoading(VIEW_LOADING);
                        //requestBanner();
                        //requestList(true, false, false, false, itemStateId, orderId, cycle_id);
                    }
                });
                break;
            case VIEW_LOADFAILURE:
                mErrorHintView.hideLoading();
                mErrorHintView.loadFailure(new ErrorHintView.OperateListener() {
                    @Override
                    public void operate() {
                        showLoading(VIEW_LOADING);
                        //requestData(true,false,cardType);
                    }
                });
                break;
            case VIEW_NODATA:
                mErrorHintView.hideLoading();
                mErrorHintView.noData(new ErrorHintView.OperateListener() {
                    @Override
                    public void operate() {

                    }
                });
                break;
            case VIEW_LOADING:
                mErrorHintView.loadingData();
                break;
        }
    }
    @Override
    public void onClick(View view) {
        switch (view.getId()){
            case R.id.choice_bank_back:
                finish();
                break;
            case R.id.choice_bank_add://添加银行卡
                Bundle bundle = new Bundle();
                bundle.putString("cardType",cardType);
                jumpToPage(AddBankcardActivity.class,bundle);
                break;
        }
    }
}
